﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bootstrap.MVC.Common;
using Bootstrap.MVC.Utils;

namespace Bootstrap.MVC.Widgets
{
    public class DropDownItemBuilder : WidgetBuilderBase<IDropDownItemModel, 
                                                         DropDownItemBuilder, 
                                                         DropDownItemHtmlGenerator, 
                                                         DropDownItemScriptGenerator>,
                                                         IDropDownSubMenuBuilder, IDropDownMenuBuilder
    {
        public DropDownItemBuilder(IDropDownMenuModel parent, IDropDownItemModel model)  : base(model)
        {
            parent.Items.Add(model);
        }


        public DropDownItemBuilder(IDropDownItemModel m)
            : base(m)
        {
        }

        //public IDropDownMenuBuilder Caption(string caption)
        //{
        //    Guard.IsNotNullOrEmpty(caption, "caption");

        //    Model.Text = caption;
        //    return this;
        //}

        public IDropDownMenuBuilder Url(string url)
        {
            Guard.IsNotNullOrEmpty(url, "url");

            Model.Url = url;
            return this;
        }

        public IDropDownMenuBuilder Divider(bool divider = true)
        {
            Model.Divider = divider;
            return this;
        }

        public IDropDownMenuBuilder ClientEvents(Action<ClientEventFactoryBase> eventAction)
        {
            Guard.IsNotNull(eventAction, "eventAction");

            var eventFactory = new ClientEventFactoryBase(Model);
            eventAction(eventFactory);

            return this;
        }

        public IDropDownSubMenuBuilder SubMenu(string caption)
        {
            this.Text(caption);
            return this;
        }

        public IDropDownSubMenuBuilder Position(LeftOrRight position = LeftOrRight.Right)
        {
            Model.Position = position;
            return this;
        }

        public IDropDownSubMenuBuilder Items(Action<DropDownItemFactory> itemAction)
        { 
            Guard.IsNotNull(itemAction, "itemAction");

            var eventFactory = new DropDownItemFactory(Model.SubMenu);
            itemAction(eventFactory);
            return this;
        }


        public IDropDownSubMenuBuilder DisableSubMenu(bool disabled = true)
        {
            Model.Disabled = disabled;
            return this;
        }

        public IDropDownMenuBuilder Disable(bool disabled = true)
        {
            Model.Disabled = disabled;
            return this;
        }

        public override DropDownItemBuilder Disabled(bool disabled = true)
        {
            return base.Disabled(disabled);
        }

        
        public new IDropDownMenuBuilder Text(string text)
        {
           base.Text(text);
           return this;
        }
    }


    public interface IDropDownMenuBuilder 
    {
        IDropDownMenuBuilder Text(string caption);
        IDropDownMenuBuilder Url(string url);
        IDropDownMenuBuilder Divider(bool divider = true);
        IDropDownMenuBuilder ClientEvents(Action<ClientEventFactoryBase> eventAction);
        IDropDownMenuBuilder Disable(bool disable = true);
        
    }

    public interface IDropDownSubMenuBuilder 
    {
        IDropDownSubMenuBuilder Items(Action<DropDownItemFactory> itemAction);
        IDropDownSubMenuBuilder Position(LeftOrRight position = LeftOrRight.Right);
        IDropDownSubMenuBuilder DisableSubMenu(bool disable = true);
    }
}
